"use strict";

var page = 1,
    defulSize = 10,
    catalIndex = 1,
    allPage = void 0,
    searchObj = void 0;
toastr.options = {
  closeButton: false,
  debug: false,
  progressBar: true,
  positionClass: "toast-top-center",
  onclick: null,
  showDuration: "300",
  hideDuration: "1000",
  timeOut: "2000",
  extendedTimeOut: "1000",
  showEasing: "swing",
  hideEasing: "linear",
  showMethod: "fadeIn",
  hideMethod: "fadeOut"
};
var typeList = [{ title: '行业春晓', id: '19', fistTit: '首页' }, 
    { title: '政策法规', id: '20', fistTit: '首页' }, 
    { title: '信息披露', id: '21', fistTit: '首页' }, 
    { title: '保险知识', id: '22', fistTit: '首页' },
     { title: '监管动态', id: '23', fistTit: '首页' },
      { title: '专家观点', id: '24', fistTit: '首页' },
       { title: '研究报告', id: '25', fistTit: '首页' }, 
       { title: '行业指数', id: '26', fistTit: '首页' }, 
       { title: '业界群英', id: '27', fistTit: '首页' }, 
       { title: '院校课题', id: '28', fistTit: '首页' }, 
       { title: '研究成果', id: '29', fistTit: '首页' }, 
       { title: '课题检索', id: '30', fistTit: '首页' },
        { title: '保险导读', id: '32', fistTit: '首页' }, 
        { title: '评论研究', id: '33', fistTit: '首页' }, 
        { title: '专题报道', id: '34', fistTit: '首页' },
        { title: '数据波澜', id: '37', fistTit: '首页' },
        { title: '风险地图', id: '387', fistTit: '首页' },
        { title: '海外风云', id: '37', fistTit: '首页' }];
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  //拿到页面参数
  var options = geeurlcodeObj();
  if(options.id == 98 || options.id == 99){
    exportLoadNews(options.id)
  }else{
    loadNews(options.id);
  }
  loadBabel(options);
});
//加载导航栏文字
function loadBabel(options) {
  $(".first_title").text(options.title);
  $("#babelTitle").text(options.text);
  // for (var i = 0; i < typeList.length; i++) {
  //   if (id == typeList[i].id) {
  //     $(".first_title").text(typeList[i].fistTit);
  //     $("#babelTitle").text(typeList[i].title);
  //     return;
  //   }
  // }
}

//根据关键字加载新闻
function loadNews(id) {
  var params = { categoryId: id };
  searchObj = params;
  params.pageNum = page;
  params.pageSize = defulSize;
  $.server(params, '/website/v1/queryCategoryContent').then(function (res) {
    var obj = res.result.data;
    $(".infoNum").text(res.result.total);
    var html = '';
    if (obj.length == 0) {
      $("#b_box").html("抱歉,没有找到相关内容");
    }
    for (var i = 0; i < obj.length; i++) {
      if (obj[i].poster) {
        html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].contentId + ",'" + $("#babelTitle").text() + "','\u66F4\u591A\u8D44\u8BAF')\"> \n                      <div class=\"img-title-box\">\n                          <img src='" + obj[i].poster + "' />\n                          <div class=\"ariticleRight\">\n                            <div class=\"content\">" + obj[i].title + "</div>\n                            <div class=\"info-time\">" + obj[i].createTime + "</div>\n                          </div>  \n                      </div>\n                  </div>";
      } else {
        //没有主图  纯内容
        html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].contentId + ",'" + $("#babelTitle").text() + "','\u66F4\u591A\u8D44\u8BAF')\">\n                      <div class=\"title-box\">\n                          <img src='images/infoDetail/infoDetail-5.png'>\n                          <div class=\"title\">" + obj[i].title + "</div>\n                      </div>\n                      <div class=\"content\">" + obj[i].createTime + "</div>\n                  </div>";
      }
    }
    $("#b_box").html(html);
    allPage = res.result.totalPage;

    getPageNav(res.result.totalPage);
  });
}

//专家进来的观点
function exportLoadNews(id) {
  var params = { contentType:id==98?1:3 };
  searchObj = params;
  params.pageNum = page;
  params.pageSize = defulSize;
  $.server(params, '/website/v1/lecturerContent').then(function (res) {
    console.log(res, 999)
    var obj = res.result.data;
    $(".infoNum").text(res.result.total);
    var html = '';
    if (obj.length == 0) {
      $("#b_box").html("抱歉,没有找到相关内容");
    }
    for (let i = 0; i < obj.length; i++) {
      if (obj[i].poster) {
        html += `<div class="info-box" onclick="goNewsDetail(${obj[i].contentId},'${$("#babelTitle").text()}','更多资讯')"> 
                      <div class="img-title-box">
                          <img src='${obj[i].poster}' />
                          <div class="ariticleRight">
                            <div class="content">${obj[i].title}</div>
                            <div class="info-time">${obj[i].createTime}</div>
                          </div>  
                      </div>
                  </div>`;
      } else { //没有主图  纯内容
        html += `<div class="info-box" onclick="goNewsDetail(${obj[i].contentId},'${$("#babelTitle").text()}','更多资讯')">
                      <div class="title-box">
                          <img src='images/infoDetail/infoDetail-5.png'>
                          <div class="title poster-title">${obj[i].title}</div>
                      </div>
                      <div class="content">${obj[i].createTime}</div>
                  </div>`;
      }
    }
    // for (var i = 0; i < obj.length; i++) {
    //   if (obj[i].poster) {
    //     html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].contentId + ",'" + $("#babelTitle").text() + "','\u66F4\u591A\u8D44\u8BAF')\"> \n                      <div class=\"img-title-box\">\n                          <img src='" + obj[i].poster + "' />\n                          <div class=\"ariticleRight\">\n                            <div class=\"content\">" + obj[i].title + "</div>\n                            <div class=\"info-time\">" + obj[i].createTime + "</div>\n                          </div>  \n                      </div>\n                  </div>";
    //   } else {
    //     //没有主图  纯内容
    //     html += "<div class=\"info-box\" onclick=\"goNewsDetail(" + obj[i].contentId + ",'" + $("#babelTitle").text() + "','\u66F4\u591A\u8D44\u8BAF')\">\n                      <div class=\"title-box\">\n                          <img src='images/infoDetail/infoDetail-5.png'>\n                          <div class=\"title\">" + obj[i].title + "</div>\n                      </div>\n                      <div class=\"content\">" + obj[i].createTime + "</div>\n                  </div>";
    //   }
    // }
    $("#b_box").html(html);
    allPage = res.result.totalPage;

    getPageNav(res.result.totalPage);
  });
}

// 计算分页 点击培训分类时  页面加载时都要调用
// 计算分页 点击培训分类时  页面加载时都要调用
function getPageNav(totalPage) {
  var html = '';
  // const len = Math.ceil(Number(length) / Number(page));
  if(totalPage>10 && page>3){
    html+='...'
  }
  if(totalPage>10){
    for (var i = 0; i < totalPage; i++) {
      if( i >= page-3 && i< page+2){
        if(i+1 == page){
          html += "<div class='pageBox active' onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
        }else{
          html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
        }
        
      } 
    }
    if(page<totalPage-3){
      html+='...'
    }
   
    if(page<totalPage-3){
      html += "<div class='pageBox' onclick= reloadNews(" + (totalPage) + ") > " + (totalPage) + " </div>";
    }
    $("#pageSizeBox").html(html);
  }else{
    for (var i = 0; i < totalPage; i++) {
      html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
    }
    $("#pageSizeBox").html(html);
    $("#pageSizeBox>div").eq(page - 1).addClass("active");
  }
  
  
 
}

//上一页
function prevPage() {
  if (page == 1) {
    return toastr.info("没有上一页了喔~");
  }
  page--;
  loadNews(searchObj.id);
}
//下一页
function nextPage() {
  if (page >= allPage) {
    return toastr.info("没有下一页了喔~");
  }
  page++;
  loadNews(searchObj.id);
}

//首页
function firstPage() {
  if (page == 1) {
    return toastr.info("已经在首页了喔~");
  }
  page = 1;
  loadNews(searchObj.id);
}

//尾页
function lastPage() {
  if (page == allPage) {
    return toastr.info("已经在尾页了喔~");
  }
  page = allPage;
  loadNews(searchObj.id);
}

// 点击GO进入任一页面
function goPage() {
  var val = $("#pageVal").val();
  if (!val || val <= 0 || val > allPage) {
    return toastr.info("不存在的页面~");
  }
  if (val == page) {
    return;
  }
  page = val;
  loadNews(searchObj.id);
}
//点击分页重新加载
function reloadNews(i) {
  page = i;
  loadNews(searchObj.id);
}
